ASP. NET Programming Simple Realization Method of Generating Static Pages [Attached with demo Source Code Download]

  • 2021-10-13 07:02:21
  • OfStack

In this paper, an example of ASP. NET programming simple realization of generating static page method. Share it for your reference, as follows:

1. Use scenarios

Static page mode can be adopted when the data of the page does not need to be changed frequently.

2. Benefits of using static pages

(1) Improve the speed of website access

(2) Reduce the burden on the server

(3) It is beneficial for search engines to crawl

3. ASP. NET generate static pages

There are many ways to generate static pages. Let's talk about one of them. References

Basic ideas:

(1) Create a template template. html file, and define a special string format in it to replace the content, such as $htmlformat

(2) Read the template and assign it to the StringBuilder object

(3) Replace the special string format with what you want

(4) Create a new static page and write the StringBuilder object to a file

4. Methodology


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
/// <summary>
///ConvertHtmlPage  Generate static pages 
/// </summary>
public class ConvertHtmlPage
{
  /// <summary>
  ///  Generate HTML Documents 
  /// </summary>
  /// <param name="templatePath"> Template path </param>
  /// <param name="templateName"> Template name </param>
  /// <param name="htmlPath"> Generate HTML Path of </param>
  /// <param name="htmlName"> Generate HTML Name of </param>
  /// <param name="format"> Contents to be replaced </param>
  /// <returns></returns>
  public static bool CreatePage(string templatePath,string templateName, string htmlPath, string htmlName,List<string> format)
  {
    try
    {
      // Read template file 
      StringBuilder htmltext = new StringBuilder();
      using (StreamReader sr = new StreamReader(templatePath+templateName))
      {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
          htmltext.AppendLine(line);
        }
        sr.Close();
      }
      // Replace HTML Marked contents in 
      for (int i = 0; i < format.Count; i++)
      {
        htmltext.Replace("$htmlformat[" + i + "]", format[i]);
      }
      // Generate HTML Documents 
      using (StreamWriter sw = new StreamWriter(htmlPath+htmlName, false, System.Text.Encoding.GetEncoding("GB2312")))
      {
        sw.WriteLine(htmltext);
        sw.Flush();
        sw.Close();
      }
    }
    catch (Exception ex)
    {
      return false;
    }
    return true;
  }
}

Attachment: DEMO example click here to download this site.

For more readers interested in asp. net related contents, please check the topics of this site: "Summary of asp. net File Operation Skills", "Summary of asp. json Operation Skills", "Summary of asp. net String Operation Skills", "Summary of asp. net Operation Skills", "Summary of asp. net ajax Skill" and "Summary of asp. net Cache Operation Skills".

I hope this paper is helpful to everyone's asp. net programming.


Related articles: